package com.das.mechanic_base.adapter.alone;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.alone.NewServiceHeaderBean;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.List;

public class X3NewAloneTitleAdapter extends RecyclerView.Adapter<X3NewAloneTitleAdapter.AloneTitleHolder> {

    private List<NewServiceHeaderBean.AllSerViceListBean.ListBeanX> mList;

    private List<Boolean> sList;

    private Context mContext;

    public X3NewAloneTitleAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
        this.sList = new ArrayList<>();
    }

    public void changeTitleList(List<NewServiceHeaderBean.AllSerViceListBean.ListBeanX> mList) {
        if (X3StringUtils.isListEmpty(mList)) {
            mList = new ArrayList<>();
        }
        this.mList = mList;
        this.sList.clear();
        for (int i = 0; i < mList.size(); i++) {
            sList.add(i == 0);
        }
        notifyDataSetChanged();
    }

    public void changeTitleSelectList(int position) {
        this.sList.clear();
        for (int i = 0; i < mList.size(); i++) {
            sList.add(i == position);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public AloneTitleHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AloneTitleHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_alone_title_item_bg,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull AloneTitleHolder holder, int position) {

        holder.tv_title.setText(mList.get(position).shopCategoryName+ "");

        if (sList.get(position)) {
            holder.tv_title.setBackground(ContextCompat.getDrawable(mContext,R.drawable.x3_title_select_bg));
            holder.tv_title.setTextColor(ContextCompat.getColor(mContext,R.color.bg_313340));
        }else {
            holder.tv_title.setBackground(null);
            holder.tv_title.setTextColor(ContextCompat.getColor(mContext,R.color.bg_707380));
        }

        holder.itemView.setOnClickListener(view -> {
            for (int i = 0; i < mList.size(); i++) {
                sList.remove(i);
                sList.add(i,i == position);
            }
            if (iOnShowTilte != null) {
                iOnShowTilte.iOnShowTitleItem(mList.get(position).shopCategoryName);
            }
            notifyDataSetChanged();
        });
    }

    IOnShowTilte iOnShowTilte;

    public void setiOnShowTilte(IOnShowTilte iOnShowTilte) {
        this.iOnShowTilte = iOnShowTilte;
    }

    public interface IOnShowTilte {
        void iOnShowTitleItem(String title);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class AloneTitleHolder extends RecyclerView.ViewHolder {
        TextView tv_title;
        public AloneTitleHolder(@NonNull View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);
        }
    }
}
